package com.webex.ta.hydra.gui.search;


import com.webex.ta.hydra.gui.action.ActionNames;
import com.webex.ta.hydra.gui.util.RolloverButton;
import com.webex.ta.hydra.gui.HydraEditorPane;
import com.webex.ta.hydra.gui.table.HydraTable;
import com.webex.ta.hydra.util.HydraUtils;
import com.webex.ta.hydra.core.TestPlan;
import com.webex.ta.hydra.core.Command;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
 * Author: Vegaz
 * Date: 2010-10-28
 * Time: 13:54:52
 */
public class SearchComponent extends JPanel {
    private static final Color FOCUS_CATCHER_COLOR = new Color(0x9999ff);

    private HydraEditorPane editor;
    private JTextField searchField;
    private JButton previous;
    private JButton next;
    private JCheckBox caseAware;
    private JCheckBox wholeWords;

    public SearchComponent(HydraEditorPane editor) {
        super(new BorderLayout());
        this.editor = editor;
        this.setFocusable(true);
        this.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                close();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        searchField = new JTextField(25) {
            protected void paintBorder(final Graphics g) {
                super.paintBorder(g);
                if (isFocusOwner()) {
                    final Rectangle bounds = getBounds();
                    g.setColor(FOCUS_CATCHER_COLOR);
                    g.drawRect(0, 0, bounds.width - 1, bounds.height - 1);
                }
            }
        };

        searchField.addFocusListener(new FocusListener() {
            public void focusGained(final FocusEvent e) {
                searchField.repaint();
            }

            public void focusLost(final FocusEvent e) {
                searchField.repaint();
            }
        });
        searchField.registerKeyboardAction(new NextOccurAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_FOCUSED);
        previous = makeToolButton(ActionNames.PREV_OCCUR, HydraUtils.getImage("previousOccurence.png"));
        next = makeToolButton(ActionNames.NEXT_OCCUR, HydraUtils.getImage("nextOccurence.png"));
        next.addActionListener(new NextOccurAction());
        caseAware = new JCheckBox("Case sensitive");
        caseAware.setFocusable(false);
        wholeWords = new JCheckBox("Whole words only");
        wholeWords.setFocusable(false);

        JPanel leadPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        leadPanel.setOpaque(false);
        leadPanel.add(searchField);
        leadPanel.add(previous);
        leadPanel.add(next);
        leadPanel.add(caseAware);
        leadPanel.add(wholeWords);
        add(leadPanel, BorderLayout.CENTER);

    }

    public void setInitialText(String text) {
        searchField.setText(text);
    }

    private void close() {
        editor.setHeaderPanel(null);
    }

    @Override
    public void requestFocus() {
        searchField.requestFocus();
    }

    private RolloverButton makeToolButton(String actionName, Icon icon) {
        RolloverButton btn = new RolloverButton(icon);
        btn.setMargin(new Insets(1, 1, 1, 1));
        btn.setBorderPainted(false);
        btn.setActionCommand(actionName);
        btn.setFocusable(false);
        return btn;
    }

    private class NextOccurAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            HydraTable table = editor.getTable();
            TestPlan tp = table.getModel().getTestPlan();
            String search = searchField.getText();
            Command start = tp.getRoot();
            if (table.getSelectedRow() >= 0) {
                start = table.getModel().getCommandAt(table.getSelectedRow());
            }

            int startCol = 0;
            int startCaret = 0;
            if (SearchContext.lastResult != null && SearchContext.lastResult.getCommand() == start) {
                startCol = SearchContext.lastResult.getColumn();
                startCaret = SearchContext.lastResult.getCaret();
            }
            FindResult fr = SearchEngine.searchFrom(search, tp, start, startCol, startCaret);
            if (fr != null) {
                SearchContext.lastResult = fr;
                table.makeVisible(fr.getCommand());
                table.getColumnModel().getSelectionModel().setSelectionInterval(fr.getColumn() + 1, fr.getColumn() + 1);
                int row = table.getModel().indexOf(fr.getCommand());
                table.scrollRectToVisible(table.getCellRect(row, fr.getColumn() +1, true));
                table.getSelectionModel().setSelectionInterval(row, row);
            } else {
                System.out.println("NOT FOUND");
            }
            return;
        }
    }
}
